package com.yu.config.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.yu.enmus.BackCodeEnum;
import com.yu.enmus.BackStatusEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.log4j.Log4j2;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author jiangting
 * description: 返回结果集公共类
 * path: com.neucloud.iot.base-BackResult
 * date: 2022/4/19
 */
@Log4j2
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class BackResult<T> implements Serializable {

    /**
     * 当前页码
     */
    @ApiModelProperty(hidden = true)
    private Integer page;

    /**
     * 总页数
     */
    @ApiModelProperty(hidden = true)
    private Integer totalPages;

    /**
     * 总条数
     */
    @ApiModelProperty(hidden = true)
    private Integer total;
    /**
     * 每页多少条记录
     */
    @ApiModelProperty(hidden = true)
    private Integer size;

    /**
     * 返回结果集
     */
    @JsonInclude
    private T data;

    /**
     * 调用状态(成功:true：失败:false)
     */
    private Boolean success;

    /**
     * 返回结果说明
     */
    private String message;


    @Override
    public String toString() {
        return "{" +
            "\"page\":" + page +
            ",\"totalPages\":" + totalPages +
            ",\"total\":" + total +
            ",\"size\":" + size +
            ",\"data\":" + data +
            ",\"message\":\"" + message +"\""+
            ",\"success\":" + success +
            "}";
    }

    private BackResult() {
    }

    private BackResult(String msg, Boolean success) {
        this.success = success;
        this.message = msg;
    }

    private BackResult(String msg, Boolean success, T data) {
        this.success = success;
        this.message = msg;
        this.data = data;
    }

    private BackResult(Boolean success, String msg, Integer pageNum, Integer totalPages, Integer totalCount, Integer pageSize, T data) {
        this.page = pageNum;
        this.totalPages = totalPages;
        this.total = totalCount;
        this.size = pageSize;
        this.data = data;
        this.success = success;
        this.message = msg;
    }

    private BackResult(Integer pageNum, Integer totalPages, Integer totalCount, Integer pageSize, T data) {
        this.page = pageNum;
        this.totalPages = totalPages;
        this.total = totalCount;
        this.size = pageSize;
        this.data = data;
    }

    /**
     * 发生错误
     * msg=程序异常
     *
     * @return BackResult对象
     */
    public static BackResult fail() {
        return new BackResult(BackCodeEnum.INVALID_REQUEST.getDescription(), BackStatusEnum.FAIL.getCode());
    }

    /**
     * 发生错误
     *
     * @param msg 错误提示信息
     * @return BackResult对象
     */
    public static BackResult fail(String msg) {
        return new BackResult(msg, BackStatusEnum.FAIL.getCode());
    }

    /**
     * 发生错误
     *
     * @param data 错误提示对象
     * @return BackResult对象
     */
    public static BackResult fail(Object data) {
        return new BackResult<Object>("请求失败", BackStatusEnum.FAIL.getCode(), data);
    }

    /**
     * 发生错误
     *
     * @param msg  错误提示信息
     * @param data 错误提示对象
     * @return BackResult对象
     */
    public static BackResult fail(String msg, Object data) {
        return new BackResult<Object>(msg, BackStatusEnum.FAIL.getCode(), data);
    }

    /**
     * 发生错误
     *
     * @param msg 错误提示信息
     * @return BackResult对象
     */
    public static BackResult fail(String msg, Boolean success) {
        return new BackResult(msg, success);
    }

    /**
     * 发生错误
     *
     * @param msg  错误提示信息
     * @param data 错误提示对象
     * @return BackResult对象
     */
    public static BackResult fail(String msg, Boolean status, Object data) {
        return new BackResult<Object>(msg, status, data);
    }


    /**
     * 查询成功调用
     * msg=请求成功
     *
     * @return BackResult对象
     */
    public static BackResult ok() {
        return new BackResult("请求成功", BackStatusEnum.SUCCESS.getCode());
    }

    /**
     * 查询成功调用
     *
     * @param msg 提示信息
     * @return BackResult对象
     */
    public static BackResult ok(String msg) {
        return new BackResult(msg, BackStatusEnum.SUCCESS.getCode());
    }

    /**
     * 查询成功调用
     *
     * @param data 成功返回对象
     * @return BackResult对象
     */
    public static BackResult<Object> ok(Object data) {
        Map<String, Object> map = new HashMap<>(1);
        if (data instanceof List || data instanceof Set) {
            map.put("content", data);
        } else {
            return new BackResult<Object>("请求成功", BackStatusEnum.SUCCESS.getCode(), data);
        }
        return new BackResult<Object>("请求成功", BackStatusEnum.SUCCESS.getCode(), map);
    }

    /**
     * 查询成功调用
     *
     * @param data 成功返回对象
     * @return BackResult对象
     */
    public static <T> BackResult<T> success(T data) {
        return new BackResult<T>("请求成功", BackStatusEnum.SUCCESS.getCode(), data);
    }

    /**
     * 查询成功调用
     *
     * @param msg  提示信息
     * @param data 成功返回对象
     * @return BackResult对象
     */
    public static BackResult<Object> ok(String msg, Object data) {
        Map<String, Object> map = new HashMap<>(1);
        if (data instanceof List || data instanceof Set) {
            map.put("content", data);
        } else {
            return new BackResult<Object>(msg, BackStatusEnum.SUCCESS.getCode(), data);
        }
        return new BackResult<Object>(msg, BackStatusEnum.SUCCESS.getCode(), map);
    }

    /**
     * 查询成功调用
     *
     * @param pageNum    当前页码
     * @param totalPages 总页数
     * @param totalCount 总条数
     * @param pageSize   每页多少条记录
     * @param data       成功返回对象
     * @param msg        提示信息
     * @return BackResult对象
     */
    public static BackResult<Object> ok(Integer pageNum, Integer totalPages, Integer totalCount, Integer pageSize, Object data, String msg) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("content", data);
        map.put("page", pageNum);
        map.put("size", pageSize);
        map.put("page_count", totalPages);
        map.put("total", totalCount);
        return new BackResult<Object>(msg, BackStatusEnum.SUCCESS.getCode(), map);
    }


    /**
     * 未登录、登陆失效、账号或密码错误
     *
     * @return BackResult对象
     */
    public static BackResult noLogin() {
        return new BackResult(BackCodeEnum.NO_LOGIN.getDescription(), BackStatusEnum.FAIL.getCode());
    }

    public static BackResult buildResult(Boolean flag) {
        if (flag) {
            return new BackResult("请求成功", BackStatusEnum.SUCCESS.getCode());
        }
        return new BackResult(BackCodeEnum.INVALID_REQUEST.getDescription(), BackStatusEnum.FAIL.getCode());
    }


    public static<T> BackResult okT(T data) {
        Map<String, T> map = new HashMap<>(1);
        if (data instanceof List || data instanceof Set) {
            map.put("content", data);
        } else {
            return new BackResult<T>("请求成功", BackStatusEnum.SUCCESS.getCode(), data);
        }
        return new BackResult<T>("请求成功", BackStatusEnum.SUCCESS.getCode(), (T) map);
    }
}
